<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.min.js"></script>
    <title>axios 发送AJAX 请求</title>
</head>
<body>
<button>GET</button>
<button>POST</button>
<button>AJAX</button>

<script>
    const btns = document.querySelectorAll("button")

    //配置 baseURl
    axios.defaults.baseURL = 'http://localhost:8000'

    btns[0].onclick = function () {
        // GET 请求
        axios.get("/axios-server", {
            params: {
                a: 100,
                b: 200
            }
        }).then(data => {
            console.log(data)
        })
    }

    btns[1].onclick = function () {
        axios.post("/axios-server", {
            username: 'admin'
        }, {
            params: {
                a: 100,
            },
            headers: {
                height: 188,
                weight: 148,
            },
        }).then(data => {
            console.log(data)
        })
    }

    btns[2].onclick = function () {
        axios({
            method: 'POST',
            url: '/axios-server',
            params: {
                vip: 100,
            },
            headers: {
                a: 100,
            },
            data: {
                name: 'feifei'
            }
        })
    }
</script>
</body>
</html>
